package com.zsj.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zsj.config.Result;
import com.zsj.domain.Contract;
import com.zsj.service.ContractService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("contract")
public class ContractController {
    @Autowired
    ContractService contractService;
    @Autowired
    RedisTemplate redisTemplate;


    @RequestMapping("list")
    public Result list(Page page, @RequestBody Contract contract) {
        Page page1 = (Page) redisTemplate.opsForValue().get("c_"+contract.getSearch()+page.getSize()+page.getCurrent());
        if (page1 == null) {
            QueryWrapper queryWrapper = new QueryWrapper();
            if (!StringUtils.isEmpty(contract.getSearch())) {
                queryWrapper.eq("cid", contract.getSearch());
                queryWrapper.or();
                queryWrapper.like("cname", contract.getSearch());
                queryWrapper.or();
                queryWrapper.like("customer", contract.getSearch());
            }
            page1 = contractService.page(page, queryWrapper);
            redisTemplate.delete("c_"+contract.getSearch()+page.getSize()+page.getCurrent());
            redisTemplate.opsForValue().setIfAbsent("c_"+contract.getSearch()+page.getSize()+page.getCurrent(), page1);
        }
        return Result.ok(page1);
    }



    @RequestMapping("delByIds")
    public Result delByIds(Integer[] ids) {
        boolean b = contractService.removeBatchByIds(Arrays.asList(ids));
        return  Result.ok(b);
    }

    @RequestMapping("upById")
    public Result upById(@RequestBody Contract contract) {
        boolean b = contractService.saveOrUpdate(contract);
        return  Result.ok(b);
    }











    @RequestMapping("listp")
    public Result listp(Page page, @RequestBody Contract contract) {
            QueryWrapper queryWrapper = new QueryWrapper();
            if (!StringUtils.isEmpty(contract.getSearch())) {
                queryWrapper.eq("cid", contract.getSearch());
                queryWrapper.or();
                queryWrapper.like("cname", contract.getSearch());
                queryWrapper.or();
                queryWrapper.like("customer", contract.getSearch());
            }
            Page page1 = contractService.page(page, queryWrapper);
        return Result.ok(page1);
    }
}
